package com.mingtai.design.pattern.creationalming.singleton;

/**
 * @author: tianmingtai
 * @create: 2021/01/07 20:42
 * @description: 双检锁/双重校验锁（DCL，即 double-checked locking）
 * 能够懒加载，采用双锁机制，安全且在多线程情况下能保持高性能。
 */

public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton lazyDoubleCheckSingleton = null;
    private LazyDoubleCheckSingleton() {

    }

    /**
     * getLazyDoubleCheckSingleton() 的性能对应用程序很关键。
     * @return
     */
    public static LazyDoubleCheckSingleton getLazyDoubleCheckSingleton() {
        if (lazyDoubleCheckSingleton == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (lazyDoubleCheckSingleton == null) {
                    lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
                }
            }
        }
        return lazyDoubleCheckSingleton;
    }

}
